package com.xuan.shop.controller;

import com.xuan.shop.dto.HttpResp;
import com.xuan.shop.entity.Car;
import com.xuan.shop.service.ICarService;
import com.xuan.shop.vo.CarTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("api/car")
@Api(tags = "汽车管理模块")
@CrossOrigin
public class CarController {
    @Autowired
    private ICarService ics;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @ApiOperation("汽车添加")
    @PutMapping("/addCar")
    public HttpResp addCar(Car car){
        ics.save(car);
        return new HttpResp(2001,"添加成功",null, LocalDateTime.now());
    }

    @ApiOperation("汽车删除")
    @DeleteMapping("/deleteById")
    public HttpResp deleteById(int id){
        ics.deleteById(id);
        return new HttpResp(2002,"删除成功",null, LocalDateTime.now());
    }

    @ApiOperation("汽车修改")
    @PostMapping("/update")
    public HttpResp update(Car car){
        ics.update(car);
        return new HttpResp(2003,"修改成功",null, LocalDateTime.now());
    }

    @ApiOperation("汽车查询")
    @GetMapping("/findAll")
    public HttpResp findAll(){
        List<Car> cars = ics.findAll();
        return new HttpResp(2004,"查询成功",cars, LocalDateTime.now());
    }

    @ApiOperation("按照品牌查询")
    @GetMapping("/findByBand")
    public HttpResp findByBand(Integer brand){
        List<Car> cars=ics.findAllByBrand(brand);
        redisTemplate.opsForValue().set("brands",brand);
        return new HttpResp(2005,"succeed",cars,LocalDateTime.now());
    }

    @ApiOperation("推荐系统")
    @GetMapping("/command")
    public HttpResp command(){
       return new HttpResp(2006,"succeed",null,LocalDateTime.now());
    }

    @ApiOperation("统计汽车类型")
    @GetMapping("/countType")
    public HttpResp countType(){
        List<CarTypeVo> carTypeVos = ics.countType();
        return new HttpResp(2007,"统计成功",carTypeVos,LocalDateTime.now());
    }
}
